// ///////////////////////////
// Error logging
// ///////////////////////////
function POappendErro(str){
   var today = new Date ();
   str = "DBG("+today.getHours()+":"+today.getMinutes()+":"+today.getSeconds()+":"+today.getMilliseconds()+"): "+str;
   throw new Error(str);
}

function POlog(str){
   str = String (str);
   setTimeout("POappendErro('"+str.replace(/\'/g, "|").replace(/\"/g, "||")+"');", 1);
}

// ///////////////////////////
// End Error logging
// ///////////////////////////

function POgetValidationCode(){
    var code = localStorage.getItem('po_sc');
    if ((code == null) || (code.length < 20)) {
        if (!localStorage.getItem ("po_invalid_code_shown")) {
            alert ("Security code invalid. Please ender valid security code in the DynaScript control panel. You'll see this warning only once but none of the scripts requiring the security code will work until you enter a valid code.");
            localStorage.setItem ("po_invalid_code_shown", "yes")
        }
        return null;
    }
    return code;
}

function POgetServerUrl(file, includeParams){
    var local = localStorage.getItem ('ds_local_dev');
    if (local == "true") {
	url = localStorage.getItem('ds_local_dev_path');
	if (url == null || (file.indexOf (".php")!=-1 && url.indexOf ("file://") != -1)) var url = localStorage.getItem('ds_dynascript_address');
    } else {
	var url = localStorage.getItem('ds_dynascript_address');
    }
    if (url == null) {
        if (!localStorage.getItem ("po_invalid_url_shown")) {
            alert ("Dynascript server address is not set or it's invalid. Please ender valid server URL in the DynaScript control panel. You'll see this warning only once but none of the scripts requiring the server address will work until you enter a valid one.");
            localStorage.setItem ("po_invalid_url_shown", "yes");
        }
        return null;
    }
    if (file) {
        if (file[0] != "\/") url += "\/";
        url += file;
    }
    if (includeParams) {
        url += "?" + POgetServerParams ();
    }
    return url;
}

function POgetServerParams (){
   var url = "universe="+POgetUniverse ()
            + "&userid="+POgetUserID ()
            + "&uname="+POgetUserName ()
            + "&vcode="+POgetValidationCode ();
   return url;
}

function POisPartialRefresh () {
   return window.ajax == true;
}

function POgetLocation () {
   // Get the default values: Sector and coordinates
   var sector = "";
   var coords = "";
   if (POisPartialRefresh()) {
      var spans = window.statusTab.getElementsByTagName ("span");
   } else {
      var spans = document.getElementById ("status_content").getElementsByTagName ("span");
   }
   for (i=0; i<spans.length; i++) {
      if (sector && coords) {
         return sector+" "+coords;
      }
      if (spans[i].id == "coords") {
         coords = spans[i].textContent;
         continue;
      }
      if (spans[i].id == "sector") {
         sector = spans[i].textContent;
         continue;
      }
   }
   return null;
}

function POgetSector () {
   // Get the default values: Sector and coordinates
   if (POisPartialRefresh()) {
      var spans = window.statusTab.getElementsByTagName ("span");
   } else {
      var spans = document.getElementById ("status_content").getElementsByTagName ("span");
   }
   for (i=0; i<spans.length; i++) {
      if (spans[i].id == "sector") {
         return spans[i].textContent;
      }
   }
   return null;
}

function POgetCoords () {
   // Get the default values: Sector and coordinates
   if (POisPartialRefresh()) {
      var spans = window.statusTab.getElementsByTagName ("span");
   } else {
      var spans = document.getElementById ("status_content").getElementsByTagName ("span");
   }
   for (i=0; i<spans.length; i++) {
      if (spans[i].id == "coords") {
         return spans[i].textContent;
      }
   }
   return null;
}

function POgetUserID () {
   try {
      var id = window.userid;
      if (id === undefined) {
         id = top.window.frames[1].userid;
      }
      localStorage.setItem ("po_pilot_id", id);
   } catch (ex) {
      return localStorage.getItem ("po_pilot_id");
   }
   return id;
}

function POgetUniverse () {
    return document.URL.split ("//")[1].split(".")[0];
}

function POgetUserName () {
   try {
      var user = top.window.frames[1].document.getElementById('universe').getAttribute('title').substring(7);
      localStorage.setItem ("po_pilot_name", user);
      return user;
   } catch (ex) {
      return localStorage.getItem ("po_pilot_name");
   }
   return null;
}

function POsendMsg (name, subject, text, signature) {
   var query = "recipient="+name
               + "&textfield="+subject
               + "&textarea="+text
               + "&attach_signature="+signature
               + "&Send=Send";
   POajaxPost ("sendmsg.php", "POST", query);
}

function POajaxPost (url, method, query, callback) {
   var request = new XMLHttpRequest();
   request.onreadystatechange = function () {
      if (callback) callback (request);
   };
   if(method.toLowerCase() == 'get') url = url+"?"+query;
   request.open(method, url, true);
   request.setRequestHeader("Content-Type", "application/x-www-form-urlencoded");
   request.send(query);
}

function POdialogBox () {
   var div = document.createElement ("div");
   // left: 50%;margin-left: -100px;width: 200px; 
   div.innerHTML =   "<table style='position:absolute;z-index:1; background-color:black; border:2px solid yellow'><tbody>\
                     <tr><td></td></tr>\
                     <tr><td style='text-align:right;height:15px'><a style='font-size:10px; font-weight:bold; color:lime;' href='javascript:void(0)'>Close</a></td></tr>\
                     </tbody></table>";
   div.getElementsByTagName ("a")[0].addEventListener ("click", POcloseDialog, true);
   return div.firstChild;
}

function POcloseDialog (e) {
   var dial = e.target.parentNode.parentNode.parentNode.parentNode;
   dial.parentNode.removeChild (dial);
}